package com.wyp168.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyp168.servicebase.exceptionhandler.GuliException;
import com.wyp168.serviceedu.entity.EduCourse;
import com.wyp168.serviceedu.entity.EduCourseDescription;
import com.wyp168.serviceedu.entity.frontVo.CourseFrontVo;
import com.wyp168.serviceedu.entity.frontVo.CourseWebVo;
import com.wyp168.serviceedu.entity.vo.*;
import com.wyp168.serviceedu.mapper.EduCourseDescriptionMapper;
import com.wyp168.serviceedu.mapper.EduCourseMapper;
import com.wyp168.serviceedu.service.EduChapterService;
import com.wyp168.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyp168.serviceedu.service.EduVideoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wyp168
 * @since 2022-05-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionMapper eduCourseDescriptionMapper;

    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;


    //添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1. 向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);

        if(insert <= 0) {
            //添加失败
            throw new GuliException(20001, "添加课程失败");
        }

        String cid = eduCourse.getId();

        //2.向课程简介表中添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        eduCourseDescriptionMapper.insert(eduCourseDescription);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseById(String courseId) {
        QueryWrapper<EduCourse> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",courseId);
        //封装课程基本信息（除简介之外的所有信息）
        EduCourse eduCourse = baseMapper.selectOne(queryWrapper1);
        QueryWrapper<EduCourseDescription> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id",courseId);
        //封装简介信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionMapper.selectOne(queryWrapper2);

        //格式化输出最终类
        CourseInfoVo finalInfo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,finalInfo);
        finalInfo.setDescription(eduCourseDescription.getDescription());
        return finalInfo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new GuliException(20001,"修改课程信息失败");
        }
        //修改简介表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionMapper.updateById(description);

    }

    //根据课程id连表查询课程基本信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public Page<EduCourse> queryByConditionPage(Integer pageNum, Integer pageSize, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        //判断条件是否为空
        if(courseQuery.getTitle() != null) {
            queryWrapper.eq("title",courseQuery.getTitle());
        }
        if(courseQuery.getStatus() != null) {
            queryWrapper.eq("status",courseQuery.getStatus());
        }
        Page<EduCourse> page = new Page<>(pageNum,pageSize);
        baseMapper.selectPage(page, queryWrapper);
        System.out.println(page);
        return page;
    }

    //删除课程的方法
    @Override
    public void deleteById(String courseId) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        //根据课程id删除课程描述
        eduCourseDescriptionMapper.deleteById(courseId);
        //根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result == 0) {
            throw new GuliException(20001,"删除失败，查无此courseId");
        }
    }

    //条件查询带分页课程信息
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        //判断Vo对象中的条件值是否为空
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {  //判断一级分类id是否存在
            queryWrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }

        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {  //判断二级分类id是否存在
            queryWrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }

        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {  //判断关注度是否存在
            queryWrapper.orderByDesc("buy_count");
        }

        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {  //判断最新时间是否存在
            queryWrapper.orderByDesc("gmt_create");
        }

        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {  //判断关注度是否存在
            queryWrapper.orderByDesc("price");
        }

        Page<EduCourse> eduCoursePage = baseMapper.selectPage(pageCourse, queryWrapper);


        List<EduCourse> records = eduCoursePage.getRecords();
        long total = eduCoursePage.getTotal();
        boolean hasNext = eduCoursePage.hasNext();
        boolean hasPrevious = eduCoursePage.hasPrevious();

        //将查询到的数据封装到map集合中，并返回
        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",eduCoursePage.getCurrent());
        map.put("pages",eduCoursePage.getPages());
        map.put("total",total);
        map.put("size",eduCoursePage.getSize());
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;

    }

    //根据课程id查询相应课程的基本信息
    @Override
    public CourseWebVo getCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);

    }
}
